import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.CategroryDao;
import com.user.Category;

public class CategroryServlet_s extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		// 获取前台数据 要显示第几页 每页显示几条数据
		int page = 0;// 第几页
		int pageCount = 10;// 每页数据条数
		int countAll = 0;// 所有页数
		// 得到前台page 判断不为空

		if (request.getParameter("page") == null || Integer.parseInt(request.getParameter("page")) < 1 ) {
			page = 1;
		} else {
			
			page = Integer.parseInt(request.getParameter("page"));
		}

		CategroryDao dao = new CategroryDao();
		List<Category> list = new ArrayList<Category>();

		// 通过总记录数/每列显示数据数 = 得到页数
		try {
			if (dao.getCount() % pageCount != 0) {
				countAll = dao.getCount() / pageCount + 1;
			} else {
				countAll = dao.getCount() / pageCount;
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			list = dao.getAnyByPage(page, pageCount);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpSession session = request.getSession();
		session.setAttribute("get", list);// 将商品信息传到前台页面
		if(page  == countAll){
			page = countAll-1;
			session.setAttribute("page", page);
		}else{
			session.setAttribute("page", page);
		}
		
		session.setAttribute("pageCount", countAll);// 得到总页数
		request.getRequestDispatcher("/productClass_s.jsp").forward(request,
				response);

	}

	
}
